package net.fornwall.eclipsecoder.preferences;

import java.util.List;

import net.fornwall.eclipsecoder.arena.DialogCloser;
import net.fornwall.eclipsecoder.languages.LanguageSupportFactory;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>,
 * we can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 */
public class PreferencesPage extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {

	public PreferencesPage() {
		super(GRID);
		setTitle("EclipseCoder");
		setPreferenceStore(EclipseCoderPlugin.getDefault().getPreferenceStore());
	}

	private StringFieldEditor userNameEditor;

	private PasswordFieldEditor passwordEditor;

	private BooleanFieldEditor autoLogonEditor;

	private BooleanFieldEditor closeDialogsEditor;

	@Override
	protected void createFieldEditors() {
		userNameEditor = new StringFieldEditor(PreferenceConstants.TC_USERNAME,
				"TopCoder username", getFieldEditorParent());
		addField(userNameEditor);
		passwordEditor = new PasswordFieldEditor(
				PreferenceConstants.TC_PASSWORD, "TopCoder password",
				getFieldEditorParent());
		addField(passwordEditor);
		addField(new SpacerFieldEditor(getFieldEditorParent()));

		autoLogonEditor = new BooleanFieldEditor(
				PreferenceConstants.TC_AUTOLOGON,
				"Auto-logon when starting the TopCoder applet",
				getFieldEditorParent());
		addField(autoLogonEditor);
		closeDialogsEditor = new BooleanFieldEditor(
				PreferenceConstants.CLOSE_DIALOGS,
				"&Close contest applet popup dialogs automatically",
				getFieldEditorParent());
		addField(closeDialogsEditor);
		addField(new SpacerFieldEditor(getFieldEditorParent()));

		// addField(new
		// RadioGroupFieldEditor(PreferenceConstants.BROWSER_LAUNCH,
		// "Problem statement browser", 1, new String[][] {
		// { "Launch external", PreferenceConstants.BROWSER_LAUNCH_EXTERNAL },
		// { "Launch in editor", PreferenceConstants.BROWSER_LAUNCH_EDITOR },
		// { "Launch in view", PreferenceConstants.BROWSER_LAUNCH_VIEW },
		// { "Do not launch", PreferenceConstants.BROWSER_LAUNCH_DONT } },
		// getFieldEditorParent(), true));

		List<String> languages = LanguageSupportFactory.supportedLanguages();
		String[][] ss = new String[languages.size()][2];
		for (int i = 0; i < ss.length; i++) {
			ss[i][0] = ss[i][1] = languages.get(i);
		}
		addField(new RadioGroupFieldEditor(
				PreferenceConstants.PREFERENCE_LANGUAGE,
				"Preferred programming language", 1, ss,
				getFieldEditorParent(), true));
	}

	@Override
	public boolean performOk() {
		if (autoLogonEditor.getBooleanValue()
				&& (userNameEditor.getStringValue().length() == 0 || passwordEditor
						.getStringValue().length() == 0)) {
			MessageDialog
					.openError(
							getShell(),
							"Incorrect",
							"You need to specify your TopCoder member name and password for auto-logon to work");
			return false;
		}
		if (EclipseCoderPlugin.shouldCloseDialogs() && !closeDialogsEditor.getBooleanValue()) {
			DialogCloser.stop();
		} else if (!EclipseCoderPlugin.shouldCloseDialogs() && closeDialogsEditor.getBooleanValue()) {
			DialogCloser.start();
		}
		return super.performOk();
	}

	public void init(IWorkbench workbench) {
		// from IWorkbenchPreferencePage interface
	}

}